
import 'dart:convert';

import 'package:flutter_test/flutter_test.dart';
import 'package:im_app/im/util/DateUtil.dart';
import 'package:im_app/im/util/DbUtil.dart';
import 'package:im_app/im/util/ImSingleton.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

void main() {
  test("insert departments", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database databaset = await DbUtil.connect("imtest.db");
    ImSingleton.getInstance().dataBase = databaset;
    Database? database = ImSingleton.getInstance().dataBase;
    Batch? batch = database?.batch();
    var data = {'currentUserId':'demo001', "code": "test", 'name': 'demo001', "status": "enable",
      "createDateTime": DateUtil.formatDate(DateTime.now()), "updateDateTime": DateUtil.formatDate(DateTime.now())};
    batch?.insert("im_department", data);
    List<Object?>? list = await batch?.commit();
    print("id: " + jsonEncode(list));
  });

  test("query departments", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("imtest.db");

    List list = await database.query("im_department");
    print("db departments : " + jsonEncode(list));
  });

  test("query imGroup", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("imtest.db");

    List list = await database.query("im_group");
    print("db im_group : " + jsonEncode(list));

    print("db im_group : " + jsonEncode(list) + ", " + list.length.toString());
  });

  test("query imGroup user", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("im.db");

    List list = await database.query("im_group_user");
    print("db im_group_user : " + jsonEncode(list) + ", " + list.length.toString());
  });

  test("query user", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("imtest.db");

    List list = await database.query("im_user");
    print("db user : " + list.length.toString());
  });

  test("query setting", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("imtest.db");

    List list = await database.query("im_setting");
    print("db setting : " + jsonEncode(list) + ", " + list.length.toString());
  });

  test("db many", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("imtest.db");
    List<Map<String, Object?>> count1 = await database.rawQuery("select count(*) from im_setting");

    print("first db : ${database.isOpen}");
    Database database2 = await DbUtil.connect("imtest.db");
    database2.close();
    List<Map<String, Object?>> count2 = await database.rawQuery("select count(*) from im_setting");
    print("first db : ${database.isOpen}");
    print("second db : ${database2.isOpen}");
  });


}